home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / raycast.zip / RAYCAST.BAS < prev    next >
BASIC Source File  |  1997-05-18  |  7KB  |  305 lines

  1. ' Raycaster 3d engine v2.1,
  2. ' created by exodus,
  3. ' By Logic Mouse.
  4. ' cloak@compuserve.com
  5. ' press f5 to start...
  6. '
  7. '
  8.  
  9.  
  10.  
  11. DECLARE SUB CreateBackground ()
  12. DECLARE SUB GetKeypress (Keycode%)
  13. CONST UpArrow = -72, DnArrow = -80, LArrow = -75, RArrow = -77
  14. ON ERROR GOTO 5
  15.  
  16. ' Default Settings
  17. ctr% = 0:    '0 = keyboard, 1 = gamepad,joystick.
  18.              'if you want joystick or gamepad, change
  19.              'this to 1 and start. it calibrates
  20.              'itself automatically.
  21.              'press A button to end the game in joystick mode.
  22.  
  23. snd% = 1:    ' 0 = off, 1 = on (this is sound)
  24.  
  25.  
  26.  
  27. Left% = STICK(0) - 5
  28. Right% = STICK(0) + 5
  29. Down% = STICK(1) - 5
  30. Up% = STICK(1) + 5
  31. CLS
  32.  
  33.  
  34. RANDOMIZE TIMER
  35. DIM Grid%(1 TO 12, 1 TO 12)
  36. DIM STable!(0 - 31 TO 360 + 32), CTable!(0 - 31 TO 360 + 32)
  37. COLOR 15, 0
  38. CLS
  39. PRINT "Enter level path name, press enter for default."
  40. LINE INPUT "Level Filename (No ext.): ", file$
  41.  
  42. lev% = 1'if a file name is entered, use it.
  43. IF file$ = "" THEN lev% = 2 'else, use the default.
  44.  
  45.  
  46.  
  47. start:
  48.  
  49.  
  50. PX! = 9: PY! = 11    'the starting coordinates of the player's location
  51. stride! = 3          'the distance covered in one "step" by the player
  52.                      '   by pressing the up or down arrow keys
  53. Heading% = 0         'the heading of the player (in degrees)
  54.  
  55. Turn% = 5            'number of degrees of rotation produced by
  56.                      '   pressing the right or left arrow keys
  57. SCREEN 7, , 0, 0
  58.  
  59. COLOR 15, 0
  60. PRINT "Loading Level"
  61. PRINT
  62. PRINT "Reading map..."
  63.  
  64. IF lev% = 1 THEN
  65. OPEN file$ + ".lev" FOR INPUT AS #1
  66. FOR Y = 1 TO 12
  67.   FOR x = 1 TO 12
  68.     INPUT #1, NUM
  69.     Grid%(x, Y) = NUM
  70.   NEXT
  71. NEXT
  72. GOTO ok
  73. END IF
  74.  
  75. IF lev% = 2 THEN
  76. FOR Y% = 1 TO 12
  77.   FOR x% = 1 TO 12
  78.     READ Grid%(x%, Y%)
  79.   NEXT
  80. NEXT
  81. GOTO ok
  82. END IF
  83.  
  84. ok:
  85. Factor! = (ATN(1) * 8) / 360
  86. FOR a% = 0 TO 359
  87.   Angle! = CSNG(a%) * Factor!
  88.   STable!(a%) = SIN(Angle!) * .1
  89.   CTable!(a%) = COS(Angle!) * .1
  90. NEXT
  91. FOR a% = -31 TO -1
  92.   STable!(a%) = STable!(a% + 360)
  93.   CTable!(a%) = CTable!(a% + 360)
  94. NEXT
  95. FOR a% = 360 TO 360 + 32
  96.   STable!(a%) = STable!(a% - 360)
  97.   CTable!(a%) = CTable!(a% - 360)
  98. NEXT
  99.  
  100.  
  101. PRINT "Creating background..."
  102. CALL CreateBackground
  103. PRINT "Computing view..."
  104. ViewPg% = 0: WorkPg% = 1: BG1% = 2: BG2% = 3
  105. SCREEN , , WorkPg%, ViewPg%
  106. GOSUB ComputeView
  107.  
  108.  
  109.  
  110.  
  111. IF ctr% = 1 THEN
  112. DO  'Main Joystick loop
  113.  
  114.  IF STRIG(1) THEN
  115.  END
  116.  END IF
  117.  
  118.  
  119. IF STICK(0) < Left% THEN
  120. Heading% = (Heading% + Turn%) MOD 360
  121. GOSUB ComputeView
  122. END IF
  123. IF STICK(0) > Right% THEN
  124. Heading% = (Heading% + (360 - Turn%)) MOD 360
  125. GOSUB ComputeView
  126. END IF
  127.  
  128. IF STICK(1) <= Down% THEN
  129. NewPX! = PX! - (STable!(Heading%) * stride!)
  130. NewPY! = PY! - (CTable!(Heading%) * stride!)
  131. IF Grid%(NewPX!, NewPY!) = 0 THEN
  132. PX! = NewPX!: PY! = NewPY!
  133. GOSUB ComputeView
  134.  
  135. ELSE 'tried to walk through a wall
  136. IF snd% = 1 THEN PLAY "t160O0L32E"
  137. END IF
  138. END IF
  139.  
  140. IF STICK(1) > Up% THEN
  141. FOR stride2! = stride! TO stride! + 1
  142. NewPX! = PX! + (STable!(Heading%) * stride2!)
  143. NewPY! = PY! + (CTable!(Heading%) * stride2!)
  144. NEXT stride2!
  145. IF Grid%(NewPX!, NewPY!) = 0 THEN
  146. PX! = NewPX!: PY! = NewPY!
  147. GOSUB ComputeView
  148.  
  149. ELSE 'tried to walk through a wall
  150. IF snd% = 1 THEN PLAY "t160O0L32E"
  151. END IF
  152. END IF
  153. LOOP
  154. SCREEN 0: WIDTH 80, 25
  155. END
  156. END IF
  157.  
  158.  
  159. IF ctr% = 0 THEN
  160. DO 'Main keyboard loop
  161.  
  162. CALL GetKeypress(Keycode%)
  163. ' keyboard buffer
  164. DEF SEG = &H40
  165. POKE &H1A, PEEK(&H1C)
  166.  
  167. SELECT CASE Keycode%
  168.   CASE LArrow
  169.     Heading% = (Heading% + Turn%) MOD 360
  170.     GOSUB ComputeView
  171.   CASE RArrow
  172.     Heading% = (Heading% + (360 - Turn%)) MOD 360
  173.     GOSUB ComputeView
  174.   CASE UpArrow
  175.     FOR stride2! = stride! TO stride! + 1
  176.     NewPX! = PX! - (STable!(Heading%) * stride2!)
  177.     NewPY! = PY! - (CTable!(Heading%) * stride2!)
  178.     NEXT stride2!
  179.  
  180.     IF Grid%(NewPX!, NewPY!) = 0 THEN
  181.       PX! = NewPX!: PY! = NewPY!
  182.       GOSUB ComputeView
  183.     ELSE 'tried to walk through a wall
  184.       IF snd% = 1 THEN SOUND 80, 1
  185.     END IF
  186.   CASE DnArrow
  187.     NewPX! = PX! + (STable!(Heading%) * stride!)
  188.     NewPY! = PY! + (CTable!(Heading%) * stride!)
  189.     IF Grid%(NewPX!, NewPY!) = 0 THEN
  190.       PX! = NewPX!: PY! = NewPY!
  191.       GOSUB ComputeView
  192.     ELSE 'tried to walk through a wall
  193.       IF snd% = 1 THEN SOUND 80, 1
  194.     END IF
  195.   CASE 27
  196.     EXIT DO
  197.   CASE ELSE
  198.     IF snd% = 1 THEN SOUND 80, 1
  199.   END SELECT
  200. LOOP
  201. SCREEN 0: WIDTH 80, 25
  202. END
  203. END IF
  204.  
  205.  
  206.  
  207. ComputeView:
  208. PCOPY BG1%, WorkPg%: SWAP BG1%, BG2%
  209. x1% = 0
  210. FOR a% = Heading% + 32 TO Heading% - 31 STEP -1
  211.   StepX! = STable!(a%): StepY! = CTable!(a%)
  212.   XX! = PX!: YY! = PY!
  213.   l% = 0
  214.   DO
  215.     XX! = XX! - StepX!: YY! = YY! - StepY!
  216.     l% = l% + 1
  217.     K% = Grid%(XX!, YY!)
  218.   LOOP UNTIL K%
  219.   DD% = 900 \ l%
  220.   h% = DD% + DD%
  221.   dt% = 100 - DD%
  222.  
  223.  
  224.  
  225.   ' basic uni colored walls;
  226.   LINE (x1%, dt%)-STEP(4, h%), K%, BF
  227.  
  228.  
  229.   ' black outline of the walls
  230.   LINE (x1%, dt%)-(x1% + 4, dt%), 0
  231.   LINE (x1%, dt% + h%)-(x1% + 4, dt% + h%), 0
  232.  
  233.  
  234.  
  235.    x1% = x1% + 5
  236. NEXT
  237. SWAP WorkPg%, ViewPg%
  238. SCREEN , , WorkPg%, ViewPg%
  239. RETURN
  240.  
  241.  
  242. ' level one data
  243. DATA  9,  4,  9,  4,  9,  4,  9,  4,  9,  4,  9,  4
  244. DATA  4,  0,  0,  0,  0,  0,  0,  0,  0,  4,  0,  4
  245. DATA  4,  0,  2, 10,  0,  0,  0,  0,  0, 12,  0,  4
  246. DATA  4,  0, 10,  2,  0,  0,  0,  0,  0,  4,  0,  9
  247. DATA  4,  0,  0,  0,  0,  0,  0,  0,  0, 12,  0,  4
  248. DATA  4,  0,  0,  0,  0,  7,  7,  0,  0,  0,  0,  9
  249. DATA  9,  0,  0,  0,  0,  7,  7,  0,  0,  0,  0,  4
  250. DATA  4,  0, 13,  0,  0,  0,  0,  1,  0,  1,  0,  9
  251. DATA  9,  0,  5,  0,  0,  0,  0,  15,  0, 15, 0,  4
  252. DATA  4,  0, 13,  0,  0,  0,  0,  1,  0,  1,  0,  9
  253. DATA  9,  0,  5,  0,  0,  0,  0,  15,  0, 15, 0,  4
  254. DATA  4,  9,  4,  9,  4,  9,  4,  9,  4,  9,  4,  9
  255. DEFINT A-Z
  256.  
  257. 5
  258. CLS
  259. COLOR 15
  260. PRINT "Bad File Name."
  261. END
  262.  
  263. DEFSNG A-Z
  264. SUB CreateBackground
  265. SCREEN , , 2, 0: CLS
  266. ' Sky
  267. LINE (0, 0)-(319, 99), 3, BF
  268. ' Clouds
  269. FOR Cnt% = 1 TO 10
  270.   x% = INT(RND * 320)
  271.   Y% = INT(RND * 80) + 10
  272.   r% = INT(RND * 50)
  273.   AR! = RND / 10
  274.   CIRCLE (x%, Y%), r%, 15, , , AR!: PAINT (x%, Y%), 15
  275. NEXT
  276. ' Sun
  277. CIRCLE (50, 30), 10, 14: PAINT (50, 30), 14, 14
  278.  
  279. ' Creates the ground texture.
  280. PCOPY 2, 3
  281. FOR Y% = 100 TO 199
  282.   FOR x% = 0 TO 319
  283.     IF RND AND 1 THEN PSET (x%, Y%), 6
  284.   NEXT
  285. NEXT
  286.  
  287. SCREEN , , 3, 0
  288. FOR Y% = 100 TO 199
  289.   FOR x% = 0 TO 319
  290.     IF RND AND 1 THEN PSET (x%, Y%), 6
  291.   NEXT
  292. NEXT
  293. 'Here ends the ground texture
  294.  
  295. SCREEN , , 0, 0
  296.  
  297. END SUB
  298.  
  299. SUB GetKeypress (Keycode%)
  300. DO: Ky$ = INKEY$
  301. : LOOP UNTIL LEN(Ky$)
  302. Keycode% = ASC(Ky$): IF Keycode% = 0 THEN Keycode% = -ASC(MID$(Ky$, 2, 1))
  303. END SUB
  304.  
  305.